package com.example.cathouse;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    List<Cat> allCats = new ArrayList<>();

    public void setAllCats(List<Cat> allCats) {
        this.allCats = allCats;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.cell_card, parent, false);
        MyViewHolder holder = new MyViewHolder(itemView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onClick(View v) {
                Cat cat = (Cat) holder.itemView.getTag();
                Bundle bundle = new Bundle();
                bundle.putInt("cat_id", cat.getId());
                NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.action_catFragment_to_cat_Detail_Fragment, bundle);
            }
        });
        return holder;
    }

    @Override
    public int getItemCount() {
        return allCats.size();
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Cat cat = allCats.get(position);
        holder.itemView.setTag(cat);
        String url = "https://636c-cloud1-5g779hyxe755f01d-1305146776.tcb.qcloud.la/";
        Glide.with(holder.itemView)
                .load(url + cat.getCatName() + "_circle.png")
                .placeholder(R.drawable.ic_loading_circle)
                .error(R.drawable.ic_error_circle)
                .into(holder.imageViewCat);
        holder.textViewCatName.setText(cat.getCatName());

    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewCat;
        TextView textViewCatName;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewCatName = itemView.findViewById(R.id.textViewCatName);
            imageViewCat = itemView.findViewById(R.id.imageViewCat);
        }
    }
}
